﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Threading;

namespace KinaestheticDebuggerEnvironment
{
    partial class Page1
    {
        Hashtable oldSnapshotInstances = new Hashtable();
        void LoadAvailableIDEs()
        {
            //Add additional IDE environments
            LoadVisualStudioInstances();
        }

        bool CompareIDEInstancesList(Hashtable oldSnapshotInstances, Hashtable snapshotInstances)
        {
            bool equalFlag = true;
            if (oldSnapshotInstances.Count == 0)
            {
                equalFlag = false;
            }
            else if (oldSnapshotInstances.Count != snapshotInstances.Count)
            {
                equalFlag = false;
            }
            else
            {
                foreach (string key in snapshotInstances.Keys)
                {
                    if (!oldSnapshotInstances.Contains(key))
                    {
                        equalFlag = false;
                    }
                }
            }
            return equalFlag;
        }

        /// <summary>
        /// Load Visual Studio Instances that are open
        /// </summary>
        void LoadVisualStudioInstances()
        {
            bool m_openSolutionsOnly = false;
            Hashtable snapshotInstances = vs.GetIDEInstances(m_openSolutionsOnly);
            Hashtable snapshotSolutions = new Hashtable();

            if (!CompareIDEInstancesList(oldSnapshotInstances,snapshotInstances))
            {
                //Clear list of IDEs
                if (!detectedIDEs.Items.IsEmpty)
                {
                    detectedIDEs.Items.Clear();
                }
                foreach (string snapshotKey in snapshotInstances.Keys)
                {
                    string solutionFile = String.Empty;
                    try
                    {
                        EnvDTE.DTE ide = (EnvDTE.DTE)snapshotInstances[snapshotKey];
                        //force to wait until the IDE is completed loaded in order to aquire the solution name
                        Thread.Sleep(1000);
                        solutionFile = ide.Solution.FullName;

                    }
                    catch { }

                    snapshotSolutions[snapshotKey] = solutionFile;

                    //Get solution name
                    string[] solutionFileTokenized = solutionFile.Split(new Char[] { '\\' });
                    string solutionName = solutionFileTokenized[solutionFileTokenized.Length - 1];

                    //Add solution to the ListBox
                    int pos = detectedIDEs.Items.Add(string.Format("{0}:{1}", snapshotKey, solutionName));
                    detectedIDEs.SelectedIndex = pos;

                    oldSnapshotInstances = snapshotInstances;
                }

            }
        }
    }
}
